/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.Collections;

import JFreeChartExtensions.chartextensions.inset.ChartInsetManager;
import JFreeChartExtensions.Series.XYExtendedIntervalSeries;
import JFreeChartExtensions.Series.XYRegressionSeries;
import JFreeChartExtensions.exceptions.MissingCellValueException;
import JFreeChartExtensions.storable.StorableSeriesObject;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;

/**
 *
 * @author Nobody
 */
public class XYReggresionSeriesCollection extends XYIntervalSeriesCollection implements Serializable, Cloneable {

    private boolean isReggressionCollection = false;
    private int datasetNumber;

    int getDatasetNumber() {
        return datasetNumber;
    }

    XYReggresionSeriesCollection(int datasetNumber) {
        this.datasetNumber = datasetNumber;
    }

    boolean isIsReggressionCollection() {
        return isReggressionCollection;
    }

    void setIsReggressionCollection(boolean isReggressionCollection) {
        this.isReggressionCollection = isReggressionCollection;
    }

    XYIntervalSeries getSeries(Comparable key) {
        return findSeries(key);
    }

    void removeSeries(Comparable key) {
        for (XYIntervalSeries series : getSeriesList()) {
            if (series.getKey().equals(key)) {
                removeSeries(series);
                break;
            }
        }
    }

    private XYIntervalSeries findSeries(Comparable key) {
        for (XYIntervalSeries series : getSeriesList()) {
            if (series.getKey().equals(key)) {
                return series;
            }
        }
        return null;
    }

    void removeAllRegressionSeries() {
        for (int i = 0; i < getSeriesCount(); ++i) {
            if (getSeries(i) instanceof XYRegressionSeries) {
                removeSeries(i--);
            }
        }
    }

    List<XYIntervalSeries> getAllRegressionSeries() {
        List<XYIntervalSeries> list = new LinkedList<XYIntervalSeries>();
        for (int i = 0; i < getSeriesCount(); ++i) {
            if (getSeries(i) instanceof XYRegressionSeries) {
                list.add(getSeries(i));
            }
        }
        return list;
    }

    List<XYIntervalSeries> getAllNonRegressionSeries() {
        List<XYIntervalSeries> list = new LinkedList<XYIntervalSeries>();
        for (int i = 0; i < getSeriesCount(); ++i) {
            if (!(getSeries(i) instanceof XYRegressionSeries)) {
                list.add(getSeries(i));
            }
        }
        return list;
    }

    void addAllSeries(XYReggresionSeriesCollection collection) {
        for (int i = 0; i < collection.getSeriesCount(); ++i) {
            addSeries(collection.getSeries(i));
        }
    }

    void add(XYIntervalSeries series) {
        super.addSeries(series);
    }

    List<XYIntervalSeries> getSeriesList() {
        List<XYIntervalSeries> list = new LinkedList<XYIntervalSeries>();
        for (int i = 0; i < getSeriesCount(); ++i) {
            list.add(getSeries(i));
        }
        return list;
    }

    boolean containsRegressionSeries() {
        for (int i = 0; i < getSeriesCount(); ++i) {
            if (getSeries(i) instanceof XYRegressionSeries) {
                return true;
            }
        }
        return false;
    }

    boolean containsOnlyOneNonRegressionSeries() {
        return (countRegressionSeries() == getSeriesCount() - 1) ? true : false;
    }

    int countRegressionSeries() {
        int counter = 0;
        for (int i = 0; i < getSeriesCount(); ++i) {
            if (getSeries(i) instanceof XYRegressionSeries) {
                ++counter;
            }
        }
        return counter;
    }

    /*void addDateSeriesForXYChart(ProgressInternalFrame xframe, ProgressInternalFrame yframe, ProgressInternalFrame eframe,
    int xColumnIdx, int yColumnIdx, int errorColumnIdx, String name, int interval) throws MissingCellValueException {


    XYExtendedIntervalSeries xys = new XYExtendedIntervalSeries(name);
    UndoableTableModel xtableModel = (UndoableTableModel) xframe.getDataTable().getModel();
    UndoableTableModel ytableModel = (UndoableTableModel) yframe.getDataTable().getModel();
    UndoableTableModel etableModel = (UndoableTableModel) eframe.getDataTable().getModel();
    boolean lineIgnored = false;
    Integer seriesNumber = 1;

    for (int i = 0; i < xtableModel.getRowCount(); i += interval) {
    Number xCell = xtableModel.getValueAsNumber(i, xColumnIdx);
    Number yCell = ytableModel.getValueAsNumber(i, yColumnIdx);

    if (xCell == null && yCell == null) {
    lineIgnored = true;
    continue;
    } else if (xCell == null || yCell == null) {
    addSeries(xys);
    if (xCell == null) {
    throw new MissingCellValueException("Missing cell value ( {" + xColumnIdx + "}{ " + i + "} )");
    } else if (yCell == null) {
    throw new MissingCellValueException("Missing cell value ( {" + yColumnIdx + "}{ " + i + "} )");
    }

    }

    if(lineIgnored){
    if(!xys.isEmpty())
    addSeries(xys);
    xys = new XYExtendedIntervalSeries(name + seriesNumber++);
    lineIgnored = false;
    }

    Float f = xCell.floatValue();
    Float g = yCell.floatValue();
    Float err = (Number) etableModel.getValueAsNumber(i, errorColumnIdx) == null ? 0 : ((Number) etableModel.getValueAsNumber(i, errorColumnIdx)).floatValue();

    xys.add(f.doubleValue(), f.doubleValue(), f.doubleValue(), g.doubleValue(), g.doubleValue() - err.doubleValue(), g.doubleValue() + err.doubleValue());
    }
    addSeries(xys);

    }

    void addDateSeriesForXYChart(ProgressInternalFrame dataframe, ProgressInternalFrame errorFrame, int xColumnIdx, int yColumnIdx, List<Integer> rows, String name, int yErrorColumnIdx, List<Integer> errRows, int interval) throws MissingCellValueException {

    XYExtendedIntervalSeries xys = new XYExtendedIntervalSeries(name);
    UndoableTableModel datatableModel = (UndoableTableModel) errorFrame.getDataTable().getModel();
    UndoableTableModel errortableModel = (UndoableTableModel) dataframe.getDataTable().getModel();
    boolean lineIgnored = false;
    Integer seriesNumber = 1;

    for (int row = 0; row < rows.size(); row += interval) {
    int i = rows.get(row);
    Number xCell = datatableModel.getValueAsNumber(i, xColumnIdx);
    Number yCell = datatableModel.getValueAsNumber(i, yColumnIdx);

    if (xCell == null && yCell == null) {
    lineIgnored = true;
    continue;
    } else if (xCell == null || yCell == null) {
    addSeries(xys);
    if (xCell == null) {
    throw new MissingCellValueException("Missing cell value ( {" + xColumnIdx + "}{ " + i + "} )");
    } else if (yCell == null) {
    throw new MissingCellValueException("Missing cell value ( {" + yColumnIdx + "}{ " + i + "} )");
    }
    }

    if(lineIgnored){
    if(!xys.isEmpty())
    addSeries(xys);
    xys = new XYExtendedIntervalSeries(name + seriesNumber++);
    lineIgnored = false;
    }
    Float f = xCell.floatValue();
    Float g = yCell.floatValue();
    Float err = 0f;
    if (row < errRows.size()) {
    int iErr = errRows.get(row);
    err = (Number) errortableModel.getValueAsNumber(iErr, yErrorColumnIdx) == null ? 0 : ((Number) errortableModel.getValueAsNumber(iErr, yErrorColumnIdx)).floatValue();
    }

    xys.add(f.doubleValue(), f.doubleValue(), f.doubleValue(), g.doubleValue(), g.doubleValue() - Math.abs(err.doubleValue()), g.doubleValue() + Math.abs(err.doubleValue()));

    }
    addSeries(xys);

    }*/
    void addAllSeries(List<XYIntervalSeries> list) {
        for (XYIntervalSeries series : list) {
            addSeries(series);
        }
    }

    void importStorableSeries(List<StorableSeriesObject> list) {
        for (StorableSeriesObject storableSeriesObject : list) {
            addSeries(storableSeriesObject.aSXYSeries());
        }
    }

    boolean isEmpty() {
        return getSeriesCount() == 0;
    }

    int getSeriesIndex(String seriesName) {
        int i = -1;
        for (XYIntervalSeries series : getSeriesList()) {
            ++i;
            if (seriesName.equals(series.getKey())) {
                return i;
            }
        }
        return -1;
    }

    int getLastAddedSeriesIndex() {
        return getSeriesCount() - 1;
    }

    XYIntervalSeries getLastAddedSeries() {
        return getSeries(getLastAddedSeriesIndex());
    }

    @Override
    public Object clone() {
        XYReggresionSeriesCollection newReggresionSeriesCollection = new XYReggresionSeriesCollection(datasetNumber);
        newReggresionSeriesCollection.setIsReggressionCollection(isReggressionCollection);
        for (XYIntervalSeries intervalSeries : getSeriesList()) {
            newReggresionSeriesCollection.addSeries(intervalSeries);
        }
        return newReggresionSeriesCollection;
    }
}
